#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<vector>

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
        int i = 0, left = -1, right = n ;
        while (i < right)
        {
            if (nums[i] == 0)
            {
                swap(nums[i++], nums[++left]);
            }
            else if (nums[i] == 1)
            {
                i++;
            }
            else
            {
                swap(nums[i], nums[--right]);
            }
        }
    }
};

class Solution2 {
public:
    vector<int> sortArray(vector<int>& nums) {
        srand(time(nullptr));
        int left = 0, right = nums.size()-1;
        qsort(nums, left, right);
        return nums;
    }

    void qsort(vector<int>& nums, int l, int r)
    {
        if (l >= r) return;
        int left = l - 1, right = r + 1, i = l;
        int key = getRandom(nums, l, r);
        while (i < right)
        {
            if (nums[i] < key)
            {
                swap(nums[i++], nums[++left]);
            }
            else if (nums[i] == key)
            {
                i++;
            }
            else
            {
                swap(nums[i], nums[--right]);
            }
        }
        qsort(nums, l, left);
        qsort(nums, right, r);
    }

    int getRandom(vector<int>& nums, int left, int right)
    {
        return nums[rand() % (right - left + 1) + left];
    }
};

class Solution3 {
public:
    int findKthLargest(vector<int>& nums, int k) {
        srand(time(nullptr));
        int left = 0, right = nums.size() - 1;
        qsort(nums, left, right, k);

    }

    int qsort(vector<int>& nums, int l, int r, int k)
    {
        if (l == r) return nums[l];
        int left = l - 1, right = r + 1, i = l;
        int key = getRandom(nums, l, r);
        while (i < right)
        {
            if (nums[i] < key)
            {
                swap(nums[i++], nums[++left]);
            }
            else if (nums[i] == key)
            {
                i++;
            }
            else
            {
                swap(nums[i], nums[--right]);
            }
        }
        int c = r - right + 1, b = right - left - 1;
        if (c >= k)
        {
            return qsort(nums, right, r, k);
        }
        else if (b + c >= k)
        {
            return key;
        }
        else
        {
            return qsort(nums, l, left, k - b - c);
        }
    }

    int getRandom(vector<int>& nums, int left, int right)
    {
        return nums[rand() % (right - left + 1) + left];
    }


};

class Solution {
public:
    vector<int> smallestK(vector<int>& nums, int k) {
        srand(time(nullptr));
        int left = 0, right = nums.size() - 1;
        qsort(nums, left, right, k);

        return { nums.begin(),nums.begin() + k };
    }

    void qsort(vector<int>& nums, int l, int r, int k)
    {
        if (l >= r) return;
        int left = l - 1, right = r + 1, i = l;
        int key = getRandom(nums, l, r);
        while (i < right)
        {
            if (nums[i] < key)
            {
                swap(nums[i++], nums[++left]);
            }
            else if (nums[i] == key)
            {
                i++;
            }
            else
            {
                swap(nums[i], nums[--right]);
            }
        }
        int a = left - l + 1, b = right - left - 1;
        if (a > k)
        {
            qsort(nums, l, left, k);
        }
        else if (a + b > k)
        {
            return;
        }
        else 
        {
            qsort(nums, right, r, k - a - b);
        }
    }

    int getRandom(vector<int>& nums, int left, int right)
    {
        return nums[rand() % (right - left + 1) + left];
    }
};

int main()
{
    vector<int> nums = { 5,1,1,2,0,0 };

    Solution3 s;
    //s.sortArray(nums);
    //s.sortColors(nums);
    return 0;
}